package org.client.otdr;

import java.awt.Dimension;
import java.util.Arrays;

import javax.swing.JFrame;

import org.openems.entity.BooleanEntity;
import org.openems.entity.Entity;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;

import framework.Client;
import framework.DomainService;
import framework.EntityStoreListener;
import framework.mashup.JsxGraph;
import framework.ui.ButtonPanel;
import framework.ui.EmsButton;
import framework.ui.EmsCheckBox;
import framework.ui.EmsComboBox;
import framework.ui.EmsEditBox;
import framework.ui.EmsEditBoxWithButton;
import framework.ui.EmsLineChart;
import framework.ui.EmsMarkerGoogleMap;
import framework.ui.EmsTable;
import framework.ui.EmsVerticalContainer;
import framework.ui.MovablePane;
import framework.ui.MovablePaneHandler;

public class OtdrClient implements Client {
	private MovablePaneHandler movablePaneHandler = new MovablePaneHandler() {

		@Override
		public void onSelect(MovablePane movablePane) {
			root.getChildren().remove(movablePane);
			root.getChildren().add(movablePane);
		}

		@Override
		public void onMove(Double x, Double y) {}
		
	};
	private AnchorPane root;
	
	@Override
	public String getTitle() {
		return "jOTDR";
	}

	@Override
	public void initialize(final DomainService service) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				createUi(service);
			}			
		});	
	}

	private void createUi(DomainService service) {
		OTDRID id = OTDRID.get();
		
		this.root = new AnchorPane();
		JFrame mainFrame = new JFrame();
		mainFrame.setSize(new Dimension(800, 640));
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JFXPanel fxPanel = new JFXPanel();
		mainFrame.add(fxPanel);	
		final Scene  scene  =  new  Scene(root, Color.ALICEBLUE);
		fxPanel.setScene(scene);
		scene.setRoot(root);
		
		if (isWallpaper()){
			OtdrMap otdrMap = new OtdrMap(service, true);
			otdrMap.getGoogleMap().prefWidthProperty().bind(root.widthProperty());
			otdrMap.getGoogleMap().prefHeightProperty().bind(root.heightProperty());
			root.getChildren().add(otdrMap.getGoogleMap());
		}
		
		EmsVerticalContainer tablePane = new EmsVerticalContainer(id.TABLEPANE, service, movablePaneHandler);
		root.getChildren().add(tablePane.getPane());
		EmsTable table = new EmsTable(id.TABLE, service);
		tablePane.add(table);
		
		EmsVerticalContainer setup = new EmsVerticalContainer(id.SETUPPANE, service, movablePaneHandler);
		setup.setOpacity(0.8);
		root.getChildren().add(setup.getPane());
		setup.add(new EmsComboBox(id.WAVELENGTH, service));
		setup.add(new EmsComboBox(id.PULSEWIDTH, service));
		setup.add(new EmsComboBox(id.DISTANCERANGE, service));
		setup.add(new EmsEditBox(id.IOR, service));
		setup.add(new EmsEditBox(id.IOR, service));
		setup.add(new EmsEditBox(id.ACTIVETRACERANGEIOR, service));
		
//		EmsVerticalContainer marker = new EmsVerticalContainer(id.MARKERPANE, service, movablePaneHandler);
//		marker.setOpacity(0.8);
//		root.getChildren().add(marker.getPane());			
	
		EmsVerticalContainer system = new EmsVerticalContainer(id.SYSTEMPANE, service, movablePaneHandler);
		system.setOpacity(0.9);
		root.getChildren().add(system.getPane());
		system.add(new EmsComboBox(id.DEVICE, service));
		system.add(new EmsButton(id.CONTROL, service));
		system.add(new EmsComboBox(id.FILELIST, service));
		system.add(new EmsButton(id.UPLOADALL, service));
		system.add(new EmsEditBox(id.SAVENAME, service));
		system.add(new EmsComboBox(id.BOOKMARK, service));
		system.add(new EmsCheckBox(id.GOOGLEMARKERENABLED, service));
		
		if (isChartEnabled()) {
			EmsVerticalContainer chartPane = new EmsVerticalContainer(id.CHARTPANE, service, movablePaneHandler);
			EmsLineChart chart = new EmsLineChart(id.TRACE, service);
			chartPane.add(chart);
			chart.addMarkerList(id.MARKERLIST);
			final EmsMarkerGoogleMap googleMarker = new EmsMarkerGoogleMap(id.GOOGLEMAPMARKER, chart, service);
			chart.addMarker(googleMarker);
			root.getChildren().add(chartPane.getPane());
			
			service.addEntityStoreListener(new EntityStoreListener() {
				@Override
				public void valueChanged(Entity entity) {
					googleMarker.setVisible(((BooleanEntity)entity).getValue());
				}
			}, id.GOOGLEMARKERENABLED);
		}
		
		if (service.isOnline()) {
			if (isReportEnabled()) {
				EmsVerticalContainer report = new EmsVerticalContainer(id.REPORTPANE, service, movablePaneHandler);
				report.add(new JsxGraph());
				root.getChildren().add(report.getPane());
			}
			
			if (isMapEnabled()) {
				EmsVerticalContainer mapPane = new EmsVerticalContainer(id.MAP, service, movablePaneHandler);
				root.getChildren().add(mapPane.getPane());
				mapPane.add(new OtdrMap(service, true).getGoogleMap());
			}
		}
		
		EmsVerticalContainer buttonPane = new EmsVerticalContainer(id.BUTTONPANE, service, movablePaneHandler);
		buttonPane.setOpacity(0.9);
		root.getChildren().add(buttonPane.getPane());
		ButtonPanel buttonPanel = new ButtonPanel(Arrays.asList(id.CONTROL, id.AUTOSEARCH), service);
		buttonPanel.addButton(new EmsEditBoxWithButton(id.SAVENAME, service));
		buttonPanel.addButton(new BrowserLauncherButton());
		
		buttonPane.add(buttonPanel);
		if (isWebClientEnabled()) {
			EmsVerticalContainer jsPane = new EmsVerticalContainer(id.JSCLIENT, service, movablePaneHandler);
			root.getChildren().add(jsPane.getPane());
			jsPane.getPane().setPrefWidth(400);
			jsPane.add(new WebClientPane(getJsServer() + "JsUi.html"));
		}
	}

	private boolean isWallpaper() {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean isWebClientEnabled() {
		return false;
	}

	private boolean isReportEnabled() {
		return false;
	}

	private boolean isChartEnabled() {
		return true;
	}

	private boolean isMapEnabled() {
		return true;
	}

	protected String getJsServer() {
		return "http://localhost:8080/";
	}

}
